package com.hencoderplus.demo1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * hencoderplus
 * Created by lzw on 2018/10/25. 14:35:11
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class CircleTextView extends View {

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    RectF rectF = new RectF();
    Rect bounds = new Rect();
    private static final String TAG = "CircleTextView";

    private static final int RADIUS = (int) Utils.dp2Px(80);

    {
        paint.setTextAlign(Paint.Align.CENTER);
    }
    public CircleTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectF.set(getWidth()/2-RADIUS,getHeight()/2-RADIUS,getWidth()/2+RADIUS,
                getHeight()/2+RADIUS);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(Utils.dp2Px(10));
        paint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawArc(rectF,120,150,false,paint);
        paint.setColor(Color.RED);
        canvas.drawArc(rectF,-90,360-150,false,paint);


        paint.setTextSize(Utils.dp2Px(40));
        paint.getTextBounds("abab",0,"abab".length(),bounds);
        Log.i(TAG, "onDraw: bounds:["+bounds.left+","+bounds.top+","+bounds.right+","
        +bounds.bottom+"]");
        int offset = (bounds.top+bounds.bottom)/2;
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawText("abab",getWidth()/2,getHeight()/2-offset,paint);
    }
}
